package com.pangu.ui.browser;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

import com.pangu.AMApplication;
import com.pangu.service.ActionException;
import com.pangu.task.mark.ATaskMark;
import com.pangu.ui.refresh.ARefreshViewController;
import com.pangu.ui.refresh.IPullable;

/**
 * 下拉刷新列表
 * 
 * @author chenxunlin01
 * 
 * @param <A>
 */
public abstract class PullListItemBrowser<A extends AMApplication> extends ListItemBrowser<A> implements IPullable {

	protected ARefreshViewController mController;

	public PullListItemBrowser(Context context, ARefreshViewController mController) {
		super(context);
		this.mController = mController;
	}

	public PullListItemBrowser(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	public boolean canPullDown() {
		int taskStatus = mTaskMark.getTaskStatus();
		if (taskStatus == ATaskMark.HANDLE_ERROR || taskStatus == ATaskMark.HANDLE_DOING) {
			return false;
		}

		if (taskStatus == ATaskMark.HANDLE_OVER && isLoadDataEmpty()) {
			return true;
		}

		View firstChild = adapterView.getChildAt(0);
		if (firstChild != null && adapterView.getFirstVisiblePosition() == 0
				&& firstChild.getTop() == adapterView.getPaddingTop()) {
			return true;

		} else {
			return false;
		}
	}

	@Override
	public boolean canPullUp() {
		return false;
	}

	@Override
	public void receiveResult(ATaskMark taskMark, ActionException exception, Object trackerResult) {
		super.receiveResult(taskMark, exception, trackerResult);
		if (taskMark.getTaskType() == ATaskMark.TYPE_REFRESH) {
			if (taskMark.getTaskStatus() == ATaskMark.HANDLE_OVER) {
				mController.refreshFinish(ARefreshViewController.SUCCEED);

			} else {
				mController.refreshFinish(ARefreshViewController.FAIL);
			}
		}
	}

}
